/*
 *  $Id: CardDescription.cs 1130 2010-09-29 11:57:53Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2009 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System.Collections.Generic;

using AgateLib.Geometry;

namespace Ragima {
    public interface IDescriptionSource {
        string DescriptionText { get; }
    }
    public sealed class CardDescription {
        private IDescriptionSource _source;
        private int _width;
        private List<string> _lines;
        private int _flavorLine;
        private bool _needUpdate;

        public CardDescription(IDescriptionSource source, int width) {
            _source = source;
            _width = width;
            _lines = new List<string>();
            _flavorLine = 0;
            _needUpdate = true;
        }

        private static UI.Font NormalFont {
            get { return Data.Fonts.CardDescription; }
        }
        private static UI.Font FlavorFont {
            get { return Data.Fonts.CardDescriptionFlavor; }
        }

        private void EnsureLines() {
            if (!_needUpdate)
                return;
            _lines.Clear();
            string text = _source.DescriptionText;
            if (string.IsNullOrEmpty(text))
                return;
            int pos = text.IndexOf('\n');
            if (pos != 0)
                WrapLines(NormalFont, pos == -1 ? text : text.Substring(0, pos));
            _flavorLine = _lines.Count;
            if (pos != -1)
                WrapLines(FlavorFont, text.Substring(pos + 1));
            _needUpdate = false;
        }

        private void WrapLines(UI.Font font, string text) {
            if (!string.IsNullOrEmpty(text))
                _lines.AddRange(font.Wrap(text, _width));
        }

        public void Update() {
            _needUpdate = true;
        }

        public void Draw(int x, int y) {
            EnsureLines();
            UI.Font font = NormalFont;
            font.Color = Color.Black;
            bool flavor = false;
            for (int i = 0; i < _lines.Count; i++) {
                if (!flavor && i >= _flavorLine) {
                    font = FlavorFont;
                    font.Color = Color.FromArgb(0x30, 0x30, 0x30);
                    flavor = true;
                }
                font.Draw(x, y, _lines[i]);
                y += 16;
            }
        }
    }
}
